package com.example.kafkademo.kafkatest;

import com.example.kafkademo.domian.InfoMsg;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *
 */
@Component
@Slf4j
public class KafkaSender {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    private Gson gson = new GsonBuilder().create();
    /**
     * 发送消息到kafka,主题为test
     */
    public void sendTest(){
        InfoMsg info = new InfoMsg();
        info.setFphone_no("13800138000");
        info.setFsystem_type("ios");
        info.setFvisit_area_code("13456");
        info.setFlac("lac9980");
        info.setFimsi("qwe34er3ew3se898");
        info.setFimei("123456789op");
        info.setFcell_id("45689");
        info.setFstart_time("2031-10-29");
        info.setFhome_area_code("家的区域类型");
        info.setFother_party("其他数据");
        info.setFcall_type("设备运作类型");
        log.info("+++++++++++++++++++++  message = {}", gson.toJson(info));
        kafkaTemplate.send("test",gson.toJson(info) + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
    }
}
